﻿Imports System.Data.SqlClient

Partial Class service_driver_preferences
    Inherits System.Web.UI.Page

    '    Dim parent = CType(Me.Master, driver_MasterPage)

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        'Make sure it's not the admin
        If Session("driver_id") Is Nothing Then
            Session("loggedin") = False
            Response.Redirect("default.aspx")
        End If

        'SqlDataSource1.SelectCommand = "Select * from COMSERV_ACTIVITY where activityid IN( Select preference_id from driver_preferences where driver_id=" & Session("driver_id") & ")"
		
		 SqlDataSource1.SelectCommand = "Select * from COMSERV_ACTIVITY INNER JOIN driver_preferences on COMSERV_ACTIVITY.activityid = driver_preferences.preference_id where driver_id=" & Session("driver_id")

		
	

        Dim day As String = Request.QueryString("day")
        Dim estring As String = Request.QueryString("e")

        If estring <> Nothing Then
            If estring = "a" Then
                errorLabel.Visible = True
                errorLabel.Text = "You are already registered for this event."
            ElseIf estring = "b" Then
                errorLabel.Visible = True
                errorLabel.Text = "Sorry but there are already too many drivers registered for this event."
            Else
                errorLabel.Visible = False
            End If
        End If

        If day <> Nothing Then
            If day = "mon" Or day = "tue" Or day = "wed" Or day = "thurs" Or day = "fri" Or day = "sat" Or day = "sun" Then
                Try
                    CSSADB_Preferences.SelectCommand = "SELECT * FROM [COMSERV_ACTIVITY] where semester_yearid=" & Session("semester") & " and [" & Request.QueryString("day") & "]='true' and [needed] != 'One-time Project'"
                Catch ex As Exception

                End Try
            Else
                CSSADB_Preferences.SelectCommand = "SELECT * FROM [COMSERV_ACTIVITY] where semester_yearid=" & Session("semester") & " and [needed] != 'One-time Project'"
            End If
        Else
            CSSADB_Preferences.SelectCommand = "SELECT * FROM [COMSERV_ACTIVITY] where semester_yearid=" & Session("semester") & " and [needed] != 'One-time Project'"
        End If

    End Sub

    Protected Sub SundayButton_Click(sender As Object, e As EventArgs) Handles SundayButton.Click
        Response.Redirect("preferences.aspx?day=" & "sun")
    End Sub
    '
    Protected Sub MondayButton_Click(sender As Object, e As EventArgs) Handles MondayButton.Click
        Response.Redirect("preferences.aspx?day=" & "mon")
    End Sub

    Protected Sub TuesdayButton_Click(sender As Object, e As EventArgs) Handles TuesdayButton.Click
        Response.Redirect("preferences.aspx?day=" & "tue")
    End Sub

    Protected Sub WednesdayButton_Click(sender As Object, e As EventArgs) Handles WednesdayButton.Click
        Response.Redirect("preferences.aspx?day=" & "wed")
    End Sub

    Protected Sub ThursdayButton_Click(sender As Object, e As EventArgs) Handles ThursdayButton.Click
        Response.Redirect("preferences.aspx?day=" & "thurs")
    End Sub

    Protected Sub FridayButton_Click(sender As Object, e As EventArgs) Handles FridayButton.Click
        Response.Redirect("preferences.aspx?day=" & "fri")
    End Sub

    Protected Sub SaturdayButton_Click(sender As Object, e As EventArgs) Handles SaturdayButton.Click
        Response.Redirect("preferences.aspx?day=" & "sat")
    End Sub

    Protected Sub AllButton_Click(sender As Object, e As EventArgs) Handles AllButton.Click
        Response.Redirect("preferences.aspx")
    End Sub


    Protected Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click

        'this disaster is what saves the preferences, but it has to check if there are already 3 preferences too
        For Each row As GridViewRow In GridView1.Rows
            Dim cb As CheckBox = CType(row.FindControl("prefCheck"), CheckBox)
            If cb IsNot Nothing AndAlso cb.Checked Then
                'response.Write("test: " & row.Cells(0).Text)
                Dim connString As String = "Data Source=sql2008.campus.jcu.edu; Initial Catalog=service; User Id=Thunder48; Password=Thundertech169;"
                Dim checkConn As SqlConnection
                Dim insertConn As SqlConnection
                Dim checkCommand As SqlCommand
                Dim insertCommand As SqlCommand
                Dim adapter As New SqlDataAdapter
                Dim count As String
                Dim insert As String
                Dim reader As SqlDataReader
                Dim sql_response As Integer
                Dim Total_response As Integer
                Dim value As Integer = row.Cells(1).Text
                count = "SELECT count(preference_id) FROM driver_preferences WHERE driver_id ='" & Session("driver_id") & "' and preference_id='" & value & "'"
                checkConn = New SqlConnection(connString)
                checkCommand = New SqlCommand(count, checkConn)
                checkCommand.CommandText = count
                checkCommand.Connection = checkConn
                Try
                    checkConn.Open()
                    reader = checkCommand.ExecuteReader()
                    'this will check how many preferences there are
                    If reader.Read() Then
                        sql_response = reader.GetInt32(0)
                        'Response.Write("data result:  " & sql_response.ToString())
                        checkConn.Close()
                        If sql_response > 0 Then
                            Response.Redirect("preferences.aspx?e=a")
                        Else
                            checkConn.Open()
                            checkCommand.CommandText = "SELECT count(preference_id) FROM driver_preferences where preference_id='" & value & "'"
                            reader = checkCommand.ExecuteReader()
                            If reader.Read() Then
                                Total_response = reader.GetInt32(0)
                                If Total_response > 2 Then
                                    Response.Redirect("preferences.aspx?e=b")
                                Else
                                    Try
                                        'if there are less than 3 preferences than add a new one
                                        'Response.Write(value)
                                        insert = "INSERT INTO driver_preferences VALUES (" & Session("driver_id") & ", " & value & ")"
                                        insertConn = New SqlConnection(connString)
                                        insertCommand = New SqlCommand(insert, insertConn)
                                        insertCommand.CommandText = insert
                                        insertCommand.Connection = insertConn
                                        insertConn.Open()
                                        insertCommand.ExecuteNonQuery()
                                        insertConn.Close()
                                        'errorLabel.Text = "Preferences saved."
                                    Catch ex As Exception
                                        'Response.Write(ex)
                                    End Try
                                End If
                                'Response.Write("number 2 result:  " & sql_response.ToString())
                            End If
                            checkConn.Close()
                        End If
                        'Response.Write(sql_response)
                    End If

                Catch ex As Exception
                    'Response.Write(ex)
                End Try
            End If
        Next row

        Response.Redirect("preferences.aspx")

    End Sub
End Class
